﻿using System;
using System.Windows;
using Hopscotch.Application.Shell.Login;
using Hopscotch.Presentation.Common.Threading;
using Hopscotch.Presentation.Configuration;
using Microsoft.Practices.Prism.PubSubEvents;

namespace Hopscotch.Application.Shell
{    
    /// <summary>
    /// Interaction logic for LoginDialog.xaml
    /// </summary>
    public partial class LoginDialog : Window
    {
        private readonly LoginViewModel loginViewModel;

        internal LoginDialog()
        {
            InitializeComponent();
        }

        internal LoginDialog(
            IEventAggregator eventAggregator, 
            IEnvironmentsConfiguration environmentsConfiguration,
            LoginInfo loginInfo)
            : this()
        {
            //The Login occurs before Prism/IoC is setup, no IoC available yet, 
            //so create the LoginViewModel manually
            loginViewModel = new LoginViewModel(eventAggregator, environmentsConfiguration, loginInfo);

            this.DataContext = loginViewModel;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
        
            //Tell the view model we are closing in case it needs to shutdown the app
            loginViewModel.Close(true);
        }

        private void Cancel_OnClick(object sender, RoutedEventArgs e)
        {
            //Tell the view model we are closing.
            loginViewModel.Close(false);
        }
        
        private void Login_OnClick(object sender, RoutedEventArgs e)
        {
            //The passwordbox doesn't support mvvm (nor should it), 
            //grab the username,password and pass to the VM
            var usr = UsernameTextBox.Text;
            var pwd = PasswordTextBox.Password;

            loginViewModel.Login(usr, pwd);
        }

        
    }
}
